// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial

import { StateLayer } from "components.slint";
import { MaterialFontSettings, MaterialPalette, Elevation } from "styling.slint";

// Default button widget with Material Design Filled Button look and feel.
export component Button {
    in property <string> text;
    in property <bool> enabled <=> i-state-layer.enabled;
    in property <bool> checkable;
    in property <image> icon;
    in property <bool> primary;
    in property <bool> colorize-icon;
    out property <bool> has-focus: i-state-layer.has-focus;
    out property <bool> pressed: self.enabled &&  i-state-layer.pressed;
    in-out property <bool> checked;

    callback clicked;

    private property <brush> text-color: root.primary ? MaterialPalette.accent-foreground : MaterialPalette.control-foreground;
    private property <float> text-opacity: 1.0;

    min-height: max(40px, i-layout.min-height);
    min-width: max(40px, i-layout.min-width);
    accessible-label: text;
    accessible-role: button;
    forward-focus: i-state-layer;

    states [
        disabled when !root.enabled : {
            i-background.background: MaterialPalette.foreground-alt;
            i-background.opacity: 0.12;
            root.text-opacity: 0.38;
            root.text-color: MaterialPalette.control-foreground;
        }
        checked when root.checked: {
            root.text-color: MaterialPalette.accent-foreground;
        }
    ]

    i-background := Rectangle {
        width: 100%;
        height: 100%;
        border-radius: 20px;
        background: root.primary ? MaterialPalette.accent-background : MaterialPalette.control-background;
        drop-shadow-color: transparent;
        drop-shadow-blur: Elevation.level0;
        drop-shadow-offset-y: 1px;
    }

    i-state-layer := StateLayer {
        has-ripple: true;
        border-radius: i-background.border-radius;
        background: MaterialPalette.foreground-alt;
        ripple-color: MaterialPalette.secondary-ripple;
        checked-background: MaterialPalette.accent-background;
        focusable: true;

        clicked => {
            if (root.checkable) {
                root.checked = !root.checked;
            }
            root.clicked();
        }
    }

    i-layout := HorizontalLayout {
        padding-left: 24px;
        padding-right: 24px;
        spacing: 8px;

        if (root.icon.width > 0 && root.icon.height > 0): Image {
            source <=> root.icon;
            width: 24px;
            opacity: root.text-opacity;
            colorize: root.colorize-icon ? root.text-color : transparent;
        }

        if (root.text != "") : Text {
            text: root.text;
            color: root.text-color;
            opacity: root.text-opacity;
            vertical-alignment: center;
            horizontal-alignment: center;
            font-weight: MaterialFontSettings.label-large.font-weight;

            animate color { duration: 250ms; easing: ease; }
        }
    }
}
